<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * flexigrid插件ajax类
 * @package  ajax/backend/system
 * @author   create by allen.qiang
 */


quanli_require('module/base/system/sysFlexigridLog.class.php');

class ajax_flexigrid {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:     //设置列宽度
                    $this->onColResize();
                    break;

                case 2:
                    $this->onDragColFlexigrid();
                    break;

                case 3:     //设置隐藏、显示列
                    $this->onToggleColFlexigrid();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }

        } else {
            returnJsonData('参数错误');
        }
    }

    /**
     * 设置列宽度
     * @return array
     */
    private function onColResize(){

        global $G_SHOP;

        if(!isset($_REQUEST['table_id']) || !is_numeric($_REQUEST['table_id'])){
            returnJsonData('表id错误');
        }

        if(!isset($_REQUEST['last_cid']) || !is_numeric($_REQUEST['last_cid'])){
            returnJsonData('初始位置错误');
        }

        if(!isset($_REQUEST['width']) || !is_numeric($_REQUEST['width'])){
            returnJsonData('列宽度错误');
        }

        try{

            $sys_flexigrid_log = new entity('sys_flexigrid_log');
            $sys_flexigrid_log->userID = $G_SHOP->currentUserID;
            $sys_flexigrid_log->table_id = $_REQUEST['table_id'];
            $sys_flexigrid_log->last_cid = $_REQUEST['last_cid'];

            if($G_SHOP->DBCA->fetch($sys_flexigrid_log)){

                $sys_flexigrid_log->width = $_REQUEST['width'];
                $G_SHOP->DBCA->save($sys_flexigrid_log);

            }

            return array('code' => 200, 'message' => 'ok');


        }catch (Exception $e){
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }


    /**
     * 拖拽触发修改列位置
     * @return array
     */
    public function onDragColFlexigrid(){

        global $G_SHOP;

        if(!isset($_REQUEST['table_id']) || !is_numeric($_REQUEST['table_id'])){
            returnJsonData('表id错误');
        }

        if(!isset($_REQUEST['cid']) || !is_numeric($_REQUEST['cid'])){
            returnJsonData('初始列位置错误');
        }

        if(!isset($_REQUEST['last_cid']) || !is_numeric($_REQUEST['last_cid'])){
            returnJsonData('目标列位置错误');
        }

        //实例化flexigrid类
        $sysFlexigridLog = new sysFlexigridLog();

        $cid = $_REQUEST['cid'];
        $last_cid = $_REQUEST['last_cid'];
        $userID = $G_SHOP->currentUserID;

        $arg_array = array(
            'table_id' => $_REQUEST['table_id'],
            'userID' => $userID,
            'last_cid' => $cid,
        );

        $flex_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flex_array){

            $flex_array = $flex_array[0];

            $G_SHOP->DBCA->beginTransaction();

            try{

                //修改所有关联列
                if($cid < $last_cid){
                    $sql = 'UPDATE sys_flexigrid_log set last_cid=last_cid-1 WHERE table_id=? AND userID=? AND last_cid > ? AND last_cid <= ?';
                    $G_SHOP->DBCA->stmt_query($sql, 'isii', $_REQUEST['table_id'], $userID, $cid, $last_cid);
                }elseif($cid > $last_cid){
                    $sql = 'UPDATE sys_flexigrid_log set last_cid=last_cid+1 WHERE table_id=? AND userID=? AND last_cid >= ? AND last_cid < ?';
                    $G_SHOP->DBCA->stmt_query($sql, 'isii', $_REQUEST['table_id'], $userID, $last_cid, $cid);
                }

                //修改指定列
                $flex_obj = new entity('sys_flexigrid_log');
                $flex_obj->userID = $userID;
                $flex_obj->table_id = $_REQUEST['table_id'];
                $flex_obj->uuid = $flex_array['uuid'];
                if($G_SHOP->DBCA->fetch($flex_obj)){
                    $flex_obj->last_cid = $_REQUEST['last_cid'];
                }

                $G_SHOP->DBCA->save($flex_obj);

                $G_SHOP->DBCA->commit();


            }catch (Exception $e){

                $G_SHOP->DBCA->rollBack();

                if ($e->getCode() > 0) {
                    return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
                } else {
                    error_log($e->getMessage());
                    return array( 'code' => 500, 'message' => '系统错误' );
                }
            }

        }

        return array('code' => 200, 'message' => 'ok');

    }


    public function onToggleColFlexigrid(){

        global $G_SHOP;

        if(!isset($_REQUEST['table_id']) || !is_numeric($_REQUEST['table_id'])){
            returnJsonData('表id错误');
        }

        if(!isset($_REQUEST['cid']) || !is_numeric($_REQUEST['cid'])){
            returnJsonData('初始列位置错误');
        }

        if(!isset($_REQUEST['hide']) || empty($_REQUEST['hide'])){
            returnJsonData('列隐藏标识错误');
        }

        try{

            $cid = $_REQUEST['cid'];
            $hide = $_REQUEST['hide'];
            $userID = $G_SHOP->currentUserID;

            $flexigrid_log = new entity('sys_flexigrid_log');
            $flexigrid_log->userID = $userID;
            $flexigrid_log->table_id = $_REQUEST['table_id'];
            $flexigrid_log->cid = $cid;

            if($G_SHOP->DBCA->fetch($flexigrid_log)){

                if($hide == 'false'){
                    $flexigrid_log->hide = 1;
                }else if($hide == 'true'){
                    $flexigrid_log->hide = 0;
                }

                $G_SHOP->DBCA->save($flexigrid_log);

            }

            return array('code' => 200, 'message' => 'ok');

        }catch (Exception $e){

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }


    }






}